package com.robot.member.controller;

import com.alibaba.fastjson.JSON;
import com.robot.member.pojo.Feedback;
import com.robot.member.service.FeedbackService;
import com.robot.member.service.MemberService;
import entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import util.CheckUtil;

import javax.servlet.http.HttpServletRequest;

/**
 * feedback控制器层
 *
 * @author
 */
@RestController
@CrossOrigin
@RequestMapping("/feedback")
public class FeedbackJsonController {

    @Autowired
    private FeedbackService feedbackService;
    @Autowired
    private MemberService memberService;

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private CheckUtil checkUtil;

    /**
     * 所有反馈并分页
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "third/findFeedbacksByDeletedAtNullOrderByCreatedAt/{page}/{size}", produces = "application/json; charset=utf-8")
    public String thirdFindFeedbacksByDeletedAtNullOrderByCreatedAt(@PathVariable int page, @PathVariable int size) {
        Page<Feedback> pageData = feedbackService.findAllByDeletedAtNullOrderByCreatedAt(page, size);
        return JSON.toJSONString(new PageResult<>(pageData.getTotalElements(), pageData.getContent()));
    }

    /**
     * 某一反馈详情
     *
     * @param id
     * @return
     */
    @GetMapping(value = "third/findFeedbackById/{id}", produces = "application/json; charset=utf-8")
    public String thirdFindFeedbackById(@PathVariable String id) {
        return JSON.toJSONString(feedbackService.findById(id));
    }

    /**
     * 修改反馈详情
     *
     * @param feedback
     * @return
     */
    @RequestMapping(value = "/third/updateFeedback", method = RequestMethod.PUT,produces = "application/json; charset=utf-8")
    public void thirdUpdateFeedback(@RequestBody String feedback) {
        feedbackService.update(JSON.parseObject(feedback, Feedback.class));
    }


    /**
     * 删除某一反馈详情
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "third/deleteFeedbackById/{id}", produces = "application/json; charset=utf-8")
    public void thirdDeleteFeedbackById(@PathVariable String id) {
        feedbackService.deleteById(id);
    }

    /**
     * 软删除
     *
     * @param id
     */
    @PutMapping(value = "third/updateFeedbackDeletedAtNullById/{id}", produces = "application/json; charset=utf-8")
    public void thirdUpdateFeedbackDeletedAtNullById(@PathVariable String id) {
        feedbackService.updateFeedbackById(id);
    }


}
